package leetcode.p543;

/***
 * 求二叉树的直径
 */
public class Solution {

    private int max = 0;

    public int solutionMethod(TreeNode root) {
        dfs(root);
        return max;
    }

    private int dfs(TreeNode root) {
        if(root == null) return 0;
        int left = dfs(root.left), right = dfs(root.right);
        max = Math.max(left + right,max);
        return Math.max(left,right) + 1;
    }

      public class TreeNode {
          int val;
          TreeNode left;
          TreeNode right;
          TreeNode(int x) { val = x; }
      }

}
